package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.kongzue.dialog.v3.WaitDialog;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.http.https.Model;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author NineTailDemonFox
 * @date 2019/8/28 14:24
 * 功能描述： 查收邀请码
 * 联系方式：1037438704@qq.com
 */
public class CheckICodeActivity extends BaseActivity {

    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_right)
    TextView textRight;
    @BindView(R.id.text_time)
    TextView text_time;
    @BindView(R.id.text_code)
    TextView textCode;
    @BindView(R.id.text_cope)
    TextView text_cope;
    @BindView(R.id.ll_gone)
    LinearLayout ll_gone;
    @BindView(R.id.text_gone)
    TextView text_gone;


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_check_icode;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        textCenter.setText("查收邀请码");
        imageleftFinish.setVisibility(View.VISIBLE);

        Model.getInstance().getUserApi().lookInvication(token)
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        if (response == null) {
                            return;
                        }
                        WaitDialog.dismiss();
                        String string = String.valueOf(response.body());
                        Map<String, String> map = JSONUtils.parseKeyAndValueToMap(string);
                        if (map == null) {
                            return;
                        }
                        Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                       if (map.get("code").equals("1")){
                           if (data == null) {
                               ll_gone.setVisibility(View.GONE);
                               text_gone.setVisibility(View.VISIBLE);
                               return;
                           }
                           ll_gone.setVisibility(View.VISIBLE);
                           text_gone.setVisibility(View.GONE);
                           textCode.setText(data.get("invite_code"));
                           text_time.setText(data.get("invite_time"));
                       }else {
                           ll_gone.setVisibility(View.GONE);
                           text_gone.setVisibility(View.VISIBLE);
                           text_gone.setText(map.get("message"));
                       }
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }

    @OnClick({R.id.image_left_finish, R.id.text_cope})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left_finish:
                finish();
                break;
            case R.id.text_cope:
                //复制
                if (TextUtils.isEmpty(textCode.getText().toString().trim())) {
                    ToastUtil.show("数据出错");
                }
                copy(textCode.getText().toString().trim());
                break;
            default:
        }
    }

}
